//
//  AppDelegate.m
//  喜马拉雅FM
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "ItemVC2.h"
#import "ItemVC3.h"
#import "ItemVC4.h"
#import "ItemVC5.h"
#import "SecendViewController.h"
#import "DataBaseHandler.h"

#import "ViewControllerForGuidePage.h"

@interface AppDelegate ()

@property (nonatomic, retain) UITabBarController *tb;


@end

@implementation AppDelegate


- (void)dealloc {

    [_tb release];
    [_window release];
    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    // 程序启动时就打开数据库.
    [[DataBaseHandler shareDataBaseHandler]open];
    [[DataBaseHandler shareDataBaseHandler]createTable];
    // 设置窗口的大小和主屏一样大.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    // 显示主窗口.
    [self.window makeKeyAndVisible];
    self.tb = [[UITabBarController alloc]init];
    [self createViewControllers];
    

    
#pragma mark -- 引导页
//    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"open"]) {
//        
//        self.window.rootViewController = self.tb;
//        
//    }else {
    
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"open"];
        
        ViewControllerForGuidePage *guide = [[ViewControllerForGuidePage alloc] init];
        self.window.rootViewController = guide;
        
        [guide release];
        
//    }
    
    [[NSNotificationCenter defaultCenter]addObserverForName:@"GuidePage" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        self.window.rootViewController = self.tb;
        
    }];
    
    [self.window release];
     [self.tb release];
    return YES;
}


- (void)createViewControllers {

    ViewController *find = [[ViewController alloc]init];
    UINavigationController *naviFind = [[UINavigationController alloc]initWithRootViewController:find];
    [find release];
    naviFind.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"grayfaxian"]tag:100];

    ItemVC2 *listen = [[ItemVC2 alloc]init];
    UINavigationController *naviListen = [[UINavigationController alloc]initWithRootViewController:listen];
    [listen release];
    naviListen.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:[UIImage imageNamed:@"graytingdan"] tag:200];
    
    ItemVC4 *download = [[ItemVC4 alloc]init];
    UINavigationController *naviDownload = [[UINavigationController alloc]initWithRootViewController:download];
    [download release];
    naviDownload.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"收藏推荐" image:[UIImage imageNamed:@"grayxiazai"] tag:400];
    
    
    ItemVC5 *me = [[ItemVC5 alloc]init];
    UINavigationController *naviMe = [[UINavigationController alloc]initWithRootViewController:me];
    naviMe.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我" image:[UIImage imageNamed:@"graywo"] tag:500];
    [me release];
    
    
    self.tb.viewControllers = @[naviFind,naviListen,naviDownload,naviMe];
    
    // tabBar只有一个,tabBarItem有5个.
    // 选中的tabBarItem颜色,默认为蓝色,此处设置为红色.
    naviFind.tabBarController.tabBar.tintColor = [UIColor redColor];
    
    [naviFind release];
    [naviListen release];
    [naviDownload release];
    [naviMe release];


}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
